//
//  airView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/28.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "airView.h"

@interface airView () <UIScrollViewDelegate>
{
    NSUInteger tem;
    __weak UILabel *tLabel;
    __weak UIButton *nowButton1, *nowButton2;
}

@property (nonatomic, strong) UIScrollView  *scrollView;

@property (nonatomic, strong) UIButton *makeHotButton;
@property (nonatomic, strong) UIButton *makeColdButton;
@property (nonatomic, strong) UIButton *dehumButton;    //除湿

@property (nonatomic, strong) UIButton *autoButton;
@property (nonatomic, strong) UIButton *blastAirButton;  //送风
@property (nonatomic, strong) UIButton *lowWindButton;
@property (nonatomic, strong) UIButton *middleWindButton;
@property (nonatomic, strong) UIButton *highWindButton;

@end

@implementation airView

- (void)setButton:(UIButton *)button withImage:(UIImage *)image Title:(NSString *)title TitleColor:(UIColor *)titleColor
{
    button.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, 35, 35)];
    imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 3);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    label.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 4 * 3);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = titleColor;
    label.text = title;
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:17];
    [button addSubview:label];
    
    [button addSubview:imageView];
    imageView = nil;
    label = nil;
}

- (void)setButton:(UIButton *)button withImage:(UIImage *)image
{
    button.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, button.bounds.size.height - 20, button.bounds.size.height - 20)];
    imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 2);
    [button addSubview:imageView];
    imageView = nil;
}

- (void)clearSubViewsOfButton:(UIButton *)button
{
    NSUInteger count = button.subviews.count;
    for (int i = 0; i < count; i++) {
        [button.subviews[0] removeFromSuperview];
    }
}

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.delegate = self;
        _scrollView.backgroundColor = BACKGROUND_COLOR;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}

- (UIButton *)makeHotButton
{
    if(!_makeHotButton){
        _makeHotButton = [[UIButton alloc] init];
        _makeHotButton.tag = 1;
        [_makeHotButton addTarget:self action:@selector(pressMakeHotOrColdButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _makeHotButton;
}

- (UIButton *)makeColdButton
{
    if(!_makeColdButton){
        _makeColdButton = [[UIButton alloc] init];
        _makeColdButton.tag = 0;
        [_makeColdButton addTarget:self action:@selector(pressMakeHotOrColdButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _makeColdButton;
}

- (UIButton *)dehumButton
{
    if(!_dehumButton){
        _dehumButton = [[UIButton alloc] init];
        _dehumButton.tag = 2;
        [_dehumButton addTarget:self action:@selector(pressMakeHotOrColdButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dehumButton;
}

- (UIButton *)autoButton
{
    if(!_autoButton){
        _autoButton = [[UIButton alloc] init];
        _autoButton.tag = 0;
        [_autoButton addTarget:self action:@selector(pressLastViewButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _autoButton;
}

- (UIButton *)blastAirButton
{
    if(!_blastAirButton){
        _blastAirButton = [[UIButton alloc] init];
        _blastAirButton.tag = 1;
        [_blastAirButton addTarget:self action:@selector(pressLastViewButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _blastAirButton;
}

- (UIButton *)lowWindButton
{
    if(!_lowWindButton){
        _lowWindButton = [[UIButton alloc] init];
        _lowWindButton.tag = 2;
        [_lowWindButton addTarget:self action:@selector(pressLastViewButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lowWindButton;
}

- (UIButton *)middleWindButton
{
    if(!_middleWindButton){
        _middleWindButton = [[UIButton alloc] init];
        _middleWindButton.tag = 3;
        [_middleWindButton addTarget:self action:@selector(pressLastViewButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _middleWindButton;
}

- (UIButton *)highWindButton
{
    if(!_highWindButton){
        _highWindButton = [[UIButton alloc] init];
        _highWindButton.tag = 4;
        [_highWindButton addTarget:self action:@selector(pressLastViewButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _highWindButton;
}

- (void)addHead
{
    UIImage *image = GET_IAMGE_WITH_NAME(@"Image/back_home_dim");
    UIImageView *backView = [[UIImageView alloc] initWithImage:image];
    [backView setFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height / 3)];
    [self.scrollView addSubview:backView];
    UIImageView *airView = [[UIImageView alloc] initWithImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/air")];
    [airView setFrame:CGRectMake(0, 0, backView.bounds.size.width / 2, backView.bounds.size.height)];
    CGFloat h = backView.bounds.size.height - airView.bounds.size.height / 2;
    airView.center = CGPointMake(backView.bounds.size.width / 2, h);
    [backView addSubview:airView];
}

- (void)addTemperature
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 3, self.bounds.size.width, 60)];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 35, 35)];
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - 45, 10, 35, 35)];
    [leftButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/temperature_down") forState:UIControlStateNormal];
    [rightButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/temperature_up") forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(pressTemDown) forControlEvents:UIControlEventTouchUpInside];
    [rightButton addTarget:self action:@selector(pressTemUp) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftButton];
    [view addSubview:rightButton];
    leftButton = nil;
    rightButton = nil;
    
    UILabel *temLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    temLabel.center = CGPointMake(view.bounds.size.width / 2, view.bounds.size.height / 2);
    temLabel.textAlignment = NSTextAlignmentCenter;
    temLabel.text = [NSString stringWithFormat:@"%lu℃",(unsigned long)tem];
    temLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:25];
    temLabel.textColor = [UIColor blueColor];
    tLabel = temLabel;
    [view addSubview:temLabel];
    temLabel = nil;
    
    [self.scrollView addSubview:view];
    view = nil;
}

- (void)addMakeHotOrCold
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 3 + 65, self.bounds.size.width, 100)];
    view.backgroundColor = BACKGROUND_COLOR;
    [self.makeColdButton setFrame:CGRectMake(0, 0, (view.bounds.size.width - 4) / 3, view.bounds.size.height)];
    [self.makeHotButton setFrame:CGRectMake((view.bounds.size.width - 4) / 3 + 2, 0, (view.bounds.size.width - 4) / 3, view.bounds.size.height)];
    [self.dehumButton setFrame:CGRectMake(view.bounds.size.width - (view.bounds.size.width - 4) / 3 , 0,
                                          (view.bounds.size.width - 4) / 3, view.bounds.size.height)];
    [view addSubview:self.makeColdButton];
    [view addSubview:self.makeHotButton];
    [view addSubview:self.dehumButton];
    [self setButton:self.makeColdButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_01_n")
              Title:@"制冷" TitleColor:TEXT_GRAY_COLOR];
    [self setButton:self.makeHotButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_02_n")
              Title:@"制热" TitleColor:TEXT_GRAY_COLOR];
    [self setButton:self.dehumButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_03_n")
              Title:@"除湿" TitleColor:TEXT_GRAY_COLOR];
    
    
    
    [self.scrollView addSubview:view];
    view = nil;
}

- (void)addLastView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 3 + 170, self.bounds.size.width, 100)];
    view.backgroundColor = BACKGROUND_COLOR;
    [self.autoButton setFrame:CGRectMake(0, 0, (view.bounds.size.width - 4) / 5, view.bounds.size.height)];
    [self.blastAirButton setFrame:CGRectMake((view.bounds.size.width - 4) / 5 + 1, 0,
                                             (view.bounds.size.width - 4) / 5, view.bounds.size.height)];
    [self.lowWindButton setFrame:CGRectMake((view.bounds.size.width - 4) / 5 * 2 + 2, 0,
                                            (view.bounds.size.width - 4) / 5, view.bounds.size.height)];
    [self.middleWindButton setFrame:CGRectMake((view.bounds.size.width - 4) / 5 * 3 + 3, 0,
                                               (view.bounds.size.width - 4) / 5, view.bounds.size.height)];
    [self.highWindButton setFrame:CGRectMake(view.bounds.size.width - (view.bounds.size.width - 4) / 5, 0,
                                             (view.bounds.size.width - 4) / 5, view.bounds.size.height)];
    [view addSubview:self.autoButton];
    [view addSubview:self.blastAirButton];
    [view addSubview:self.lowWindButton];
    [view addSubview:self.middleWindButton];
    [view addSubview:self.highWindButton];
    
    [self setButton:self.autoButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_auto_n") Title:@"自动" TitleColor:TEXT_GRAY_COLOR];
    [self setButton:self.blastAirButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_normal_n") Title:@"送风" TitleColor:TEXT_GRAY_COLOR];
    [self setButton:self.lowWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_low_n") Title:@"低风" TitleColor:TEXT_GRAY_COLOR];
    [self setButton:self.middleWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_midd_n") Title:@"中风" TitleColor:TEXT_GRAY_COLOR];
    [self setButton:self.highWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_high_n") Title:@"高风" TitleColor:TEXT_GRAY_COLOR];
    
    [self.scrollView addSubview:view];
    view = nil;
}

- (void)addTurnOnAndTurnOff
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 3 + 275, self.bounds.size.width, 80)];
    view.backgroundColor = BACKGROUND_COLOR;
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, (view.bounds.size.width - 1) / 2, view.bounds.size.height)];
    [self setButton:leftButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnon")];
    [leftButton addTarget:self action:@selector(pressAirOn) forControlEvents:UIControlEventTouchUpInside];
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake((view.bounds.size.width - 1) / 2 + 1, 0,
                                                                       (view.bounds.size.width - 1) / 2, view.bounds.size.height)];
    [self setButton:rightButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnoff")];
    [rightButton addTarget:self action:@selector(pressAirOff) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftButton];
    [view addSubview:rightButton];
    
    [self.scrollView addSubview:view];
    view = nil;
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height / 3 + 355);
}

#pragma mark - 按钮响应函数
- (void)pressAirOn
{
    [self.delegate sendRespondByFuctionName:AIR_ON];
}

- (void)pressAirOff
{
    [self.delegate sendRespondByFuctionName:AIR_OFF];
}

- (void)pressTemUp
{
    //温度升
    tem ++;
    tLabel.text = [NSString stringWithFormat:@"%lu℃",(unsigned long)tem];
}

- (void)pressTemDown
{
    //温度降
    tem --;
    tLabel.text = [NSString stringWithFormat:@"%lu℃",(unsigned long)tem];
}

- (void)pressMakeHotOrColdButton:(UIButton *)button
{
    if(nowButton1 != nil){
        if(nowButton1.tag == 0){
            [self clearSubViewsOfButton:self.makeColdButton];
            [self setButton:self.makeColdButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_01_n")
                      Title:@"制冷" TitleColor:TEXT_GRAY_COLOR];
        }else if(nowButton1.tag == 1){
            [self clearSubViewsOfButton:self.makeHotButton];
            [self setButton:self.makeHotButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_02_n")
                      Title:@"制热" TitleColor:TEXT_GRAY_COLOR];
        }else{
            [self clearSubViewsOfButton:self.dehumButton];
            [self setButton:self.dehumButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_03_n")
                      Title:@"除湿" TitleColor:TEXT_GRAY_COLOR];
        }
    }
    if(button.tag == 0) {
        nowButton1 = self.makeColdButton;
        [self clearSubViewsOfButton:self.makeColdButton];
        [self setButton:self.makeColdButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_01_p")
                  Title:@"制冷" TitleColor:TEXT_GREEN_COLOR];
        [self.delegate sendRespondByFuctionName:AIR_MAKECOLD];
    }else if(button.tag == 1){
         nowButton1 = self.makeHotButton;
        [self clearSubViewsOfButton:self.makeHotButton];
        [self setButton:self.makeHotButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_02_p")
                  Title:@"制热" TitleColor:TEXT_GREEN_COLOR];
        [self.delegate sendRespondByFuctionName:AIR_MAKEHOT];
    }else{
         nowButton1 = self.dehumButton;
        [self clearSubViewsOfButton:self.dehumButton];
        [self setButton:self.dehumButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/airconditioning_mode_03_p")
                  Title:@"除湿" TitleColor:TEXT_GREEN_COLOR];
        [self.delegate sendRespondByFuctionName:AIR_DEHUM];
    }
}

- (void)pressLastViewButton:(UIButton *)button
{
    if(nowButton2 != nil){
        if(nowButton2.tag == 0){
            [self clearSubViewsOfButton:self.autoButton];
            [self setButton:self.autoButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_auto_n") Title:@"自动" TitleColor:TEXT_GRAY_COLOR];
        }else if(nowButton2.tag == 1){
            [self clearSubViewsOfButton:self.blastAirButton];
            [self setButton:self.blastAirButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_normal_n") Title:@"送风" TitleColor:TEXT_GRAY_COLOR];
        }else if(nowButton2.tag == 2){
            [self clearSubViewsOfButton:self.lowWindButton];
            [self setButton:self.lowWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_low_n") Title:@"低风" TitleColor:TEXT_GRAY_COLOR];
        }else if(nowButton2.tag == 3){
            [self clearSubViewsOfButton:self.middleWindButton];
            [self setButton:self.middleWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_midd_n") Title:@"中风" TitleColor:TEXT_GRAY_COLOR];
        }else{
            [self clearSubViewsOfButton:self.highWindButton];
            [self setButton:self.highWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_high_n") Title:@"高风" TitleColor:TEXT_GRAY_COLOR];
        }
    }
    if(button.tag == 0){
        nowButton2 = self.autoButton;
        [self clearSubViewsOfButton:self.autoButton];
        [self setButton:self.autoButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_auto_p") Title:@"自动" TitleColor:TEXT_GREEN_COLOR];
        [self.delegate sendRespondByFuctionName:AIR_AUTO];
    }else if(button.tag == 1){
        nowButton2 = self.blastAirButton;
        [self clearSubViewsOfButton:self.blastAirButton];
        [self setButton:self.blastAirButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_normal_p") Title:@"送风" TitleColor:TEXT_GREEN_COLOR];
        [self.delegate sendRespondByFuctionName:AIR_WIPE];
    }else if(button.tag == 2){
        nowButton2 = self.lowWindButton;
        [self clearSubViewsOfButton:self.lowWindButton];
        [self setButton:self.lowWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_low_p") Title:@"低风" TitleColor:TEXT_GREEN_COLOR];
        //低风
    }else if(button.tag == 3){
        nowButton2 = self.middleWindButton;
        [self clearSubViewsOfButton:self.middleWindButton];
        [self setButton:self.middleWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_midd_p") Title:@"中风" TitleColor:TEXT_GREEN_COLOR];
        //中风
    }else{
        nowButton2 = self.highWindButton;
        [self clearSubViewsOfButton:self.highWindButton];
        [self setButton:self.highWindButton withImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/air/wind_high_p") Title:@"高风" TitleColor:TEXT_GREEN_COLOR];
        //高风
    }
}

- (instancetype) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.scrollView];
        tem = 17;
        [self addHead];
        [self addTemperature];
        [self addMakeHotOrCold];
        [self addLastView];
        [self addTurnOnAndTurnOff];
    }
    
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
